<template>
  <el-card>
    <template #header>个人中心</template>

    <div>
      <!-- 1.0 展示个人信息 -->
      <ul>
        <li>
          <!-- <input type="file" /> -->
          <span>用户头像：</span>
          <p class="avatar">
            <img :src="users.img_url" alt="" />
          </p>
          <!-- element-ui 的上传组件可以直接指定一个服务器位置即可 -->
          <el-upload
            class="avatar-uploader"
            action="http://wxopen.besthuishenghuo.com/api/file/pictureUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li>
          <span>账号名称：</span>
          <p>{{ users.username }}</p>
        </li>
        <li>
          <span>用户昵称：</span>
          <p>{{ users.nickname }}</p>
        </li>
        <li>
          <span>用 户 I D：</span>
          <p>{{ users.member_id }}</p>
        </li>
        <li>
          <span>创建时间：</span>
          <p>{{ users.create_time }}</p>
        </li>

        <li class="btn">
          <el-button type="primary" size="small" @click="saveAvatar"
            >保存头像</el-button
          >
        </li>
      </ul>
    </div>
  </el-card>
</template>

<script>
import { avatarSave } from '../../api/users.js'

export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      avatarUrl: ''
    }
  },
  created() {
    this.users = JSON.parse(localStorage.getItem('users'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // 处理前端本地预览的功能，并不是实质性的上传
      this.imageUrl = URL.createObjectURL(file.raw)

      // 把响应回来的内容，主要是图片id存储起来，方便下一次请求
      this.avatarUrl = res.data.id
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    async saveAvatar() {
      if (this.avatarUrl === '') return ''

      const { code, msg } = await avatarSave({
        member_id: this.users.member_id,
        access_token: this.users.access_token,
        head_img_id: this.avatarUrl
      })

      if (code === 1) {
        this.$message.success(msg)
        // location.reload()
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style lang="less" scoped>
ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  display: flex;
  align-items: center;

  &.btn {
    padding-top: 20px;
    padding-left: 40px;
  }

  span {
    width: 100px;
    text-align: right;
    margin-right: 10px;
  }

  .avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    border: 2px solid #f4f4f4;

    img {
      width: 100%;
      transform: scale(1.2);
    }
  }
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  margin-left: 40px;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 68px;
  height: 68px;
  line-height: 68px;
  text-align: center;
}
.avatar {
  width: 68px;
  height: 68px;
  display: block;
}
</style>
